package com.example.xln111.controller;

import com.example.xln111.model.Student;
import com.example.xln111.service.StudentService;
import com.example.xln111.util.GeneralResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping("/students")
    public GeneralResponse students() {
        GeneralResponse response = new GeneralResponse();
        response.setSuccess(true);
        response.setData(studentService.getAllStudents());
        return response;
    }


    @PostMapping("/student/add")
    public GeneralResponse add(@RequestParam String name, @RequestParam String sex, @RequestParam String classname){
        Student student =new Student();
        student.setSex(sex);
        student.setName(name);
        student.setClassname(classname);

        GeneralResponse response = new GeneralResponse();
        response.setSuccess(true);
        response.setData(studentService.add(student));
        return response;
    }

    @PutMapping("/student/update")
    public GeneralResponse update(@RequestParam String name,@RequestParam String sex,@RequestParam String classname,@RequestParam  int id){
        Student student =new Student();
        student.setId(id);
        student.setSex(sex);
        student.setName(name);
        student.setClassname(classname);

        GeneralResponse response = new GeneralResponse();
        response.setSuccess(true);
        response.setData(studentService.update(student));
        return response;
    }

    @PostMapping("/student/delete")
    public GeneralResponse delete(@RequestParam int id){
        GeneralResponse response = new GeneralResponse();
        response.setSuccess(true);
        response.setData(studentService.deleteById(id));

        return response;
    }


    @GetMapping("/student")
    public GeneralResponse studentById(@RequestParam int id){
        GeneralResponse response = new GeneralResponse();
        response.setSuccess(true);
        List<Student> result = new ArrayList<>();
        result.add(studentService.getById(id));
        response.setData(result);
        return response;
    }
}
